package org.example.iris.controller;

import org.example.iris.dto.JobInfo;
import org.example.iris.dto.RecommendCompanyInfo;
import org.example.iris.dto.Result;
import org.example.iris.entity.*;
import org.example.iris.service.CompanyService;
import org.example.iris.service.UserService;
import org.example.iris.utils.Md5Util;
import org.example.iris.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    CompanyService companyService;

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    public Result register(@RequestBody Map<String, String> params) {
        if (userService.findByUsername(params.get("username")) != null) {
            return Result.error("用户名已存在");
        }
        if (userService.findByPhone(params.get("phoneNum")) != null) {
            return Result.error("手机号已绑定");
        }
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();

        if (ops.get(params.get("phoneNum")) == null || !params.get("code").equals(ops.get(params.get("phoneNum")))) {
            return Result.error("验证码错误");
        }
        if(!params.get("password").equals(params.get("repassword"))){
            return Result.error("两次密码不一致");
        }
        //新建user
        User user = new User();
        user.setUsername(params.get("username"));
        user.setPassword(Md5Util.getMD5String(params.get("password")));
        user.setPhone(params.get("phoneNum"));
        user.setRole("company");

        //新建company
        Company company = new Company();
        company.setCompanyName(params.get("companyName"));
        company.setDescription(params.get("description"));
        company.setCulture(params.get("culture"));
        company.setLogo(params.get("logo"));
        company.setWebsite(params.get("website"));
        company.setCity(params.get("city"));
        company.setExaminePassed(false);

        companyService.register(user, company);
        return Result.success();
    }

    @GetMapping("/recommend")
    public Result<List<RecommendCompanyInfo>> recommend() {
        List<RecommendCompanyInfo> recommendCompanyInfos = companyService.recommendCompany();
        return Result.success(recommendCompanyInfos);
    }

    @GetMapping("/selectCompany")
    public Result<List<Company>> selectCompany(@RequestBody Map<String, String> params) {
        List<Company> company = companyService.selectCompany(params.get("keyword"));
        return Result.success(company);
    }

    @GetMapping("/companyJob")
    public Result<List<JobInfo>> companyJob(Integer companyId) {
        List<JobInfo> jobs = companyService.companyJob(companyId);
        return Result.success(jobs);
    }

    /**
     * 更新企业信息
     * @param companyId 企业ID
     * @param companyName 企业名称
     * @param description 企业描述
     * @param culture 企业文化
     * @param logo 企业logo
     * @param website 企业官网
     * @param city 企业所在城市
     * @return 更新结果
     */
    @PutMapping("/update")
    public String updateCompanyInfo(
            @RequestParam("companyId") Integer companyId,
            @RequestParam("companyName") String companyName,
            @RequestParam("description") String description,
            @RequestParam("culture") String culture,
            @RequestParam("logo") String logo,
            @RequestParam("website") String website,
            @RequestParam("city") String city) {

        // 创建公司对象
        Company company = new Company();
        company.setCompanyId(companyId);
        company.setCompanyName(companyName);
        company.setDescription(description);
        company.setCulture(culture);
        company.setLogo(logo);
        company.setWebsite(website);
        company.setCity(city);

        // 调用服务层更新企业信息
        boolean isUpdated = companyService.updateCompanyInfo(company);

        // 返回更新结果
        if (isUpdated) {
            return "企业信息更新成功";
        } else {
            return "企业信息更新失败";
        }
    }
    @GetMapping("/companyInfo")
    public Result<Company> companyInfo(Integer companyId) {
        if(companyId == null) {
            Map<String, Object> claims = ThreadLocalUtil.get();
            Integer userId = (Integer) claims.get("userId");
            Company company = companyService.findByUserId(userId);
            companyId = company.getCompanyId();
        }
        Company company = companyService.companyInfo(companyId);
        return Result.success(company);
    }

    @GetMapping("/allCompany")
    public Result<List<Company>> allCompany() {
        List<Company> company = companyService.allCompany();
        return Result.success(company);
    }
}
